package com.candy.test;

import com.candy.bean.UserInfo;
import com.candy.dao.UserInfoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class DBDriver {
    UserInfoMapper mapper=null;
    SqlSession   session=null;
    @Before
    public void before() {
        //        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
            mapper=session.getMapper(UserInfoMapper.class);
        }catch (Exception e){

        }
    }

    @Test
    public  void test01() {
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            UserInfo user =  mapper.selectByPrimaryKey(2);
            System.out.println(user);
    }
    @Test
    public void test02(){
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("梅花10");
        userInfo.setUserpwd("123456");
        userInfo.setUseradress("天津");
        userInfo.setUsertel("16354789874");
       int num= mapper.insertSelective(userInfo);
        System.out.println(num);
        session.commit();
    }
    @Test
    public void test03(){
        UserInfo userInfo=new UserInfo();
        userInfo.setId(49);
        userInfo.setUseradress("内蒙古");
        userInfo.setUsertel("13698745874");
        int num= mapper.updateByPrimaryKeySelective(userInfo);
        System.out.println(num);
        session.commit();
    }
    @Test
    public void test04(){
        int num= mapper.deleteByPrimaryKey(39);
        System.out.println(num);
        session.commit();
    }



}
